package com.wu.Backstage.config;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.wu.Backstage.check.MiddlelayerCheck;
import com.wu.Backstage.entity.UserAccount;
import com.wu.Backstage.entity.easy.EasyUserAccounts;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;


public class EasyPoiConfig {

    public List<EasyUserAccounts> getUserAccounts(){
        List<UserAccount> user= MiddlelayerCheck.oftenCheck.getAllUsers();
        List<EasyUserAccounts> easyUser = new ArrayList<>();
        for (UserAccount entity : user){
            EasyUserAccounts easyUserAccounts = new EasyUserAccounts();
            try {
                BeanUtils.copyProperties(easyUserAccounts, entity);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            easyUser.add(easyUserAccounts);
        }
        return easyUser;
    }


    //导出excel
    public void putexcel() throws IOException {
        List<EasyUserAccounts> user= getUserAccounts();
        //参数1.导出对象的配置文件名等，参数二:导出的类型 参数三:导出的数据
        Workbook workbook =  ExcelExportUtil.exportExcel(new ExportParams("用户信息列表","用户信息"),EasyUserAccounts.class,user);
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\用户信息汇总表.xls");
        workbook.write(outputStream);
        outputStream.close();
        outputStream.close();
    }

}
